// miniprogram/pages/search/search.js
const db = wx.cloud.database()
const suggestResultCollection = db.collection("suggestResult")   // 引入suggestResult库
Page({
  data: {
    suggest_list:[],
    value:'',
    isSuggestShow: true,
  },
  onSearch(e){
    let dataList = [],
    data = e.detail;
    dataList.push(data);
    let storageList = wx.getStorageSync("title")
    if(storageList){
      dataList = dataList.concat(storageList);
    }
    wx.setStorageSync('title', dataList);
  },
  async onChange(e) {
    let dataList = wx.getStorageSync('title');
    let searchKey = e.detail;
    // console.log("searchKey",searchKey);
    let {data} =await suggestResultCollection.where({
      desc:db.RegExp({
        regexp:searchKey,
      })
    })
    .get()
    this.setData({
      suggest_list:data,
      isSuggestShow: true,
      dataList
    })
    if(!e.detail){
      this.setData({
        isSuggestShow: false
      })
    }
  },
  delete_btn(){
    // console.log("ee");
    let dataList = [];
    wx.setStorageSync('title',dataList)
    this.setData({
      dataList
    })
  },
  async onLoad (options) {
    let dataList = wx.getStorageSync('title')
    let {value} = options;
    let  {data}  = await suggestResultCollection
    .where({
      desc:db.RegExp({
        regexp:value
      })
    })
    .get();
    this.setData({
      value,
      suggest_list:data,
      dataList
    })
  },
  rollback_btn(){
    wx.navigateTo({
      url: '../datails/details',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})